#include "c_detect.h"

Bool isAtFinish(Hero hero, Rectangle finish){
	int hero_x1 = hero.sprite->x;
	int hero_y1 = hero.sprite->y;
	int hero_x2 = hero.sprite->x + hero.sprite->width;
	int hero_y2 = hero.sprite->y + hero.sprite->height;
	int finish_x1 = finish.x;
	int finish_y1 = finish.y;
	int finish_x2 = finish.x + finish.width;
	int finish_y2 = finish.y + finish.height;
	
	if(hero_x2 >= finish_x1
		&& hero_x1 <= finish_x2
		&& hero_y2 >= finish_y1
		&& hero_y1 <= finish_y2)
		return true;
	return false;
}

Bool isColidingEnemy(Hero hero, Enemy enemy){
	int hero_x1 = hero.sprite->x;
	int hero_y1 = hero.sprite->y;
	int hero_x2 = hero.sprite->x + hero.sprite->width;
	int hero_y2 = hero.sprite->y + hero.sprite->height;
	int enemy_x1 = enemy.sprite->x;
	int enemy_y1 = enemy.sprite->y;
	int enemy_x2 = enemy.sprite->x + enemy.sprite->width;
	int enemy_y2 = enemy.sprite->y + enemy.sprite->height;
	
	if(hero_x2 >= enemy_x1
		&& hero_x1 <= enemy_x2
		&& hero_y2 >= enemy_y1
		&& hero_y1 <= enemy_y2)
		return true;
	return false;
}

Bool isColidingFloor(Hero hero, char *bg){
		int lin,col;
		for(lin=hero.sprite->y ; lin < (hero.sprite->y+hero.sprite->height) ; lin++)
			for(col=hero.sprite->x ; col < (hero.sprite->x+hero.sprite->width) ; col++)
				if(bg[lin*HRES + col]==2)
					return true;
		return false;
}

Bool isPlatformAbove(Hero hero, char *bg){
	int col;
	int yAboveHero = 0;
	if(hero.sprite->y != 0)
		yAboveHero = hero.sprite->y - 10;
		
	for(col = hero.sprite->x ; col < hero.sprite->x + hero.sprite->width ; col++)
		if(bg[yAboveHero * HRES + col] == 2)
			return true;
	return false;
}

Bool isPlatformBelow(Hero hero, char *bg){
	int yBelowHero = hero.sprite->y + hero.sprite->height + 5;
	int col;
	for(col = hero.sprite->x ; col < hero.sprite->x + hero.sprite->width ; col++)
		if(bg[yBelowHero * HRES + col] == 2)
			return true;
	return false;
}

Bool isStoneCollidingBg(Stone stone, char* bg){
	int lin,col;
	for(lin=stone.sprite->y ; lin < (stone.sprite->y+stone.sprite->height) ; lin++)
		for(col=stone.sprite->x ; col < (stone.sprite->x+stone.sprite->width) ; col++)
			if(bg[lin*HRES + col]==2)
				return true;
	return false;
}

Bool isStoneCollidingEnemy(Stone stone, Enemy enemy){
	int stone_x1 = stone.sprite->x;
	int stone_y1 = stone.sprite->y;
	int stone_x2 = stone.sprite->x + stone.sprite->width;
	int stone_y2 = stone.sprite->y + stone.sprite->height;
	int enemy_x1 = enemy.sprite->x;
	int enemy_y1 = enemy.sprite->y;
	int enemy_x2 = enemy.sprite->x + enemy.sprite->width;
	int enemy_y2 = enemy.sprite->y + enemy.sprite->height;
	
	if(stone_x2 >= enemy_x1
		&& stone_x1 <= enemy_x2
		&& stone_y2 >= enemy_y1
		&& stone_y1 <= enemy_y2)
		return true;
	return false;
}
